﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public partial class SalesOrderItem
    {
        partial void ItemSum_Compute(ref decimal result)
        {
            // Set result to the desired field value
            result = (decimal)((this.ItemQty == null ? 0 : this.ItemQty) * (this.ItemPrice == null ? 0 : this.ItemPrice));
        }

        partial void ItemRecipeName_Compute(ref string result)
        {
            // Set result to the desired field value
            string fullName = string.Empty;
            foreach (var recipe in this.ItemRecipes)
            {
                if (recipe != null && recipe.RecipeGroup != null && recipe.RecipeDependency != null && recipe.RecipeDependency.ChildProduct != null)
                    fullName += (fullName == string.Empty ? string.Empty : ", ") + recipe.RecipeGroup.ProductGroupName + " (" + recipe.RecipeDependency.ChildProduct.ProductName + ")";
            }
            result = fullName;
        }

        partial void ItemProductGroup_Changed()
        {
            if (this.ItemRecipes != null && this.ItemRecipes.Count() > 0 && this.ItemProductGroup != null)
            {
                var parentRecipeGroup = this.ItemRecipes.Where(ir => ir.ParentRecipeGroup.Id == this.ItemProductGroup.Id).FirstOrDefault();
                if (parentRecipeGroup == null)
                {
                    foreach (var itemRecipe in this.ItemRecipes)
                    {
                        itemRecipe.Delete();
                    }
                    if (this.ItemProduct != null)
                    {
                        this.ItemProduct = null;
                    }
                }
            }

            if (this.ItemProductGroup != null && this.ItemRecipes.Count() == 0)
            {
                LoadParentChildRecipeGroups(this.ItemProductGroup);
            }
        }

        private void LoadParentChildRecipeGroups(ProductGroup parentGroup)
        {
            foreach (var recipeGroup in parentGroup.ChildProductGroups.OrderBy(rg => rg.ProductGroupName))
            {
                SalesOrderItemRecipe itemRecipe = this.ItemRecipes.AddNew();
                itemRecipe.RecipeGroup = recipeGroup;
                itemRecipe.ParentRecipeGroup = parentGroup;

                LoadParentChildRecipeGroups(itemRecipe.RecipeGroup);
            }
        }

        partial void ItemProduct_Changed()
        {
            foreach (var item in this.ItemRecipes)
            {
                if (item.ParentRecipeGroup != null && this.ItemProductGroup != null & item.ParentRecipeGroup.Id == this.ItemProductGroup.Id)
                {
                    item.ParentRecipeProduct = this.ItemProduct;
                }
            }
        }


        partial void ItemRecipeCode_Changed()
        {
            if (!string.IsNullOrEmpty(ItemRecipeCode)) //user defined code
            {
                try
                {
                    ItemRecipeCode = ItemRecipeCode.Replace(" ", "").Replace("_", "-").Replace(",", ".").Trim();
                    string[] groups = ItemRecipeCode.Split('.');
                    string[] rootGroupLexem = groups[0].Split('-');
                    ItemProductGroup = this.DataWorkspace.SalesDepData.ProductGroups.Where(pg => pg.ProductGroupCode == rootGroupLexem[0]).FirstOrDefault();
                    if (ItemProductGroup != null)
                    {
                        ItemProduct = ItemProductGroup.Products.Where(pr => pr.ProductCode == rootGroupLexem[1]).FirstOrDefault();
                        if (ItemProduct != null)
                        {
                            for (int i = 1; i < groups.Length; i++)
                            {
                                string[] recipeGroupLexem = groups[i].Split('-');
                                if (recipeGroupLexem.Length == 2)
                                {
                                    var itemRecipe = ItemRecipes.Where(ir => ir.RecipeGroup.ProductGroupCode == recipeGroupLexem[0]).FirstOrDefault();
                                    if (itemRecipe != null && itemRecipe.RecipeGroup.ProductGroupCode == recipeGroupLexem[0] && itemRecipe.ParentRecipeProduct != null)
                                    {
                                        itemRecipe.RecipeDependency = this.DataWorkspace.SalesDepData.RecipeDependencies.Where(rd =>
                                            rd.ParentRecipeProduct.ProductCode == itemRecipe.ParentRecipeProduct.ProductCode
                                            && rd.ChildProductGroup.ProductGroupCode == recipeGroupLexem[0]
                                            && rd.ChildProduct.ProductCode == recipeGroupLexem[1]
                                            ).FirstOrDefault();
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                }
            }
            else//build code automatically
            {
                string result = string.Empty;
                if (ItemProductGroup != null && ItemProduct != null)
                {
                    result = ItemProductGroup.ProductGroupCode + "-" + ItemProduct.ProductCode;
                    foreach (var itemRecipe in ItemRecipes.OrderBy(rg => rg.RecipeGroup.ProductGroupCode))
                    {
                        if (itemRecipe.RecipeGroup != null && itemRecipe.RecipeDependency != null && itemRecipe.RecipeDependency.ChildProduct.ProductCode != null)
                            result += "." + itemRecipe.RecipeGroup.ProductGroupCode + "-" + itemRecipe.RecipeDependency.ChildProduct.ProductCode;
                    }
                }
                ItemRecipeCode = result;
            }

        }

        partial void RowNumber_Compute(ref short result)
        {
        }

    }
}
